/*******************************************************************
REPLICATION FILE FOR:
Paper: “Class, Ethnicity, and Cooperation Among Women: Evidence from a Public Goods Experiment in Lebanon”
Authors: Leslie Marshall and Laura Paler

This do file replicates the missing data imputation in which we run 10 rounds of missing
data imputation using predictive mean matching and Stata's ICE command.

NOTE: As described in the README document that accompanies this replication, at some point
there was a snafu with the seed such as the dataset produced below is not the exact same
as the one that was used in the actual article. The results are very similar, however. We invite
readers to run this file and then the subsequent Leb_PGG_3_Clean.do and Leb_PGG_4_Analyze.do to 
check the consistency of our results.
********************************************************************/

*******************************************************
*SET GLOBAL DIRECTORIES
*******************************************************

*Set your personal working directory here: cd~
cd ~/Dropbox/LEBANON/WRITING/3_PGG/REPLICATION

	global dos "DOs_and_ADOs"
	global packages "DOs_and_ADOs/PACKAGES"

	global data_orig "DATA/ORIG"
	global data_newimp "DATA/NEW_IMPUTE"
	global working "DATA/NEW_IMPUTE/WORKING"

	global results "RESULTS" 
	global results_figs "RESULTS/FIGS"
	global results_tables "RESULTS/TABLES"

	set more off

	use "$data_newimp/Leb_1_Prep_FINAL.dta", clear	

*Installing version controlled packages
	adopath ++ "$packages"
	do "$packages/ice.ado"
	
***************************************
* Creating macros for imputation
***************************************
	global c_scr_cat scr_q5_regdistrict_REC1 scr_q7_iga_final_REC4 scr_q8_sect scr_recruiter_id
	global c_scr_pmm_demo scr_q1_gender_REC2 scr_q2_age scr_q3_marital_REC1 scr_q4_educ_REC1 scr_q6_work_REC1 
	global c_scr_pmm_econ scr_q9_assets_REC1 scr_q10_hh_area scr_q11_summer_hh scr_q12_poweralt_code scr_q13_vacay_code scr_q14_dineout_REC1 scr_q15_income scr_q16_code_aud scr_q16_income_subj_REC1 scr_total_pts scr_econ_status
	global c_scr_pmm_all $c_scr_pmm_demo $c_scr_pmm_econ
	
	global c_scr_neighb_dum neighb_achrafiyeh neighb_bourj neighb_dahyi neighb_hadath neighb_kornish neighb_sin neighb_tarik
	global c_pre_cat pre_q15_a_concern_1_REC4 pre_q15_b_concern_2_REC4 pre_q15_c_concern_3_REC4 pre_q3_sect_REC3 
	global c_pre_pmm_demo pre_q2_age pre_q9_class_REC3b 
	global c_pre_pmm_econ pre_q8_income_REC1 pre_q10_diffecon_REC1 
	global c_pre_pmm_conn pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1
	global c_pre_pmm_sect pre_q4_changesect_REC1 pre_q5_diffsect_REC1 pre_q12_a_marry_REC1 pre_q12_b_doctor_REC1 pre_q12_c_neighbor_REC1 pre_q12_d_poltalk_REC1 pre_q12_e_econtalk_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 
	global c_pre_pmm_polact pre_q7_a_friends_REC1 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 pre_q13_disagree_REC1 pre_q6_polparty_REC3
	global c_pre_pmm_id pre_q14_a_agegrp_REC1 pre_q14_b_gender_REC1 pre_q14_c_confession_REC1 pre_q14_d_class_REC1 pre_q14_e_lebanese_REC1 pre_q14_f_occupation_REC1 
	global c_pre_pmm_all $c_pre_pmm_demo $c_pre_pmm_econ $c_pre_pmm_conn $c_pre_pmm_sect $c_pre_pmm_polact $c_pre_pmm_id
	
	global y_post_pmm_disc1 post_q1_a_express_REC1 post_q1_b_justified_REC1 post_q1_c_selfish_REC1 post_q1_d_hiding_REC1 post_q1_e_changemind_REC1 post_q1_f_serious_REC1 post_q1_g_learned_REC1 post_q1_h_empthem_REC1 post_q1_i_empyou_REC1 post_q1_j_anxious_REC1
	global y_post_pmm_disc2 post_q2_simconcerns_REC1 post_q3_surprise_econ_REC1 post_q4_agreechange_REC1 post_q5_surprise_sect_REC1 post_q6_overall_REC1 post_q7_knowppl_REC1
	global y_post_pmm_selfcat post_q8_a_low_sun_REC1 post_q8_b_low_chr_REC1 post_q8_c_low_shi_REC1 post_q8_d_upp_sun_REC1 post_q8_e_upp_chr_REC1 post_q8_f_upp_shi_REC1 post_q14_domidentity_REC1b
	global y_post_pmm_groupdist post_q9_a_lchr_uchr_REC1 post_q9_b_lsun_usun_REC1 post_q9_c_lshi_ushi_REC1 post_q9_d_lshi_lsun_REC1 post_q9_e_lsun_lchr_REC1 post_q9_f_lchr_lshi_REC1 post_q9_g_ushi_usun_REC1 post_q9_h_usun_uchr_REC1 post_q9_i_uchr_ushi_REC1
	global y_post_pmm_xpress post_q10_a_enemies_REC1 post_q10_b_worry_REC1 post_q10_c_unsure_REC1
	global y_post_pmm_policy post_q11_electionlaw_REC1 post_q12_marriage_REC1
	global y_post_pmm_polact post_q13_a_talksame_REC1 post_q13_b_talkother_REC1 post_q13_c_petition_REC1 post_q13_d_protestsame_REC1 post_q13_e_protestother_REC1 post_q13_f_talkelites_REC1 post_q13_g_ngo_REC1
	global y_post_pmm_all $y_post_pmm_disc1 $y_post_pmm_disc2 $y_post_pmm_selfcat $y_post_pmm_groupdist $y_post_pmm_xpress $y_post_pmm_policy $y_post_pmm_polact
	
	global y_pet_pmm_all pet_allsign pet_sign_REC1
	global y_pgg_pmm_all pgr_q1_uncond pgr_q2_scen_1 pgr_q2_scen_2 pgr_q2_scen_3 pgr_q2_scen_4 pgr_q2_scen_5 pgr_q2_scen_6 pgr_q2_scen_7 pgr_q2_scen_8 pgr_q2_scen_9 pgr_q2_scen_10 pgr_q2_scen_11 pgg_rd1_totalpot pgg_rd2_totalpot pgg_rd1 pgg_rd2 
	global y_map1 me_beirut_REC1 me_baalbek_REC1 me_hasbaya_REC1 me_hermel_REC1 me_rashaya_REC1 me_westbekaa_REC1 me_zahle_REC1 me_aley_REC1 me_baabda_REC1 me_chouf_REC1 me_jbeil_REC1 me_keserwan_REC1 me_metn_REC1 me_bintjbeil_REC1 me_marjayoun_REC1 me_nabatieh_REC1 me_akkar_REC1 me_batroun_REC1 me_becharre_REC1 me_koura_REC1 me_minieh_REC1 me_tripoli_REC1 me_zgharta_REC1 me_jezzine_REC1 me_saida_REC1 
	global y_map2 me_sour_REC1
	global y_map_all $y_map1 $y_map2
	
*Summarize the missing data	
	misstable sum $c_scr_cat $c_pre_cat $c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map_all, all

	
************************
*IMPUTATION
************************


*Save the pre-imputation version of the dataset
	save "$working/Leb_2_impute_0_6.dta", replace

*The seed is the date this do file was created
	version 15.1
	sort pid
	set seed 20161018

	forvalues i = 1/10 {
		use "$working/Leb_2_impute_0_6.dta", clear
		ice i.set i.pre_q3_sect_REC3 $c_scr_pmm_all $c_pre_pmm_all [pweight=wgt_strat2] , cmd($c_scr_pmm_all $c_pre_pmm_all: regress) match m(1)  saving("$working/Leb_2_impute_`i'_1.dta", replace)   
		use "$working/Leb_2_impute_`i'_1.dta", clear
		keep if _mj==1
		rename _mj _mj1
		rename _mi _mi1
		ice $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1  i.set i.pre_q3_sect_REC3 ms_q1_mod_id  $c_scr_pmm_all $c_pre_pmm_all [pweight=wgt_strat2] if T_mixed_sect==1 & T_mixed_class==0, cmd($c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1: regress) match m(1)  saving("$working/Leb_2_impute_`i'_2.dta", replace)   
		use "$working/Leb_2_impute_`i'_2.dta", clear
		keep if _mj==1
		rename _mj _mj2
		rename _mi _mi2
		ice $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1  i.set i.pre_q3_sect_REC3 ms_q1_mod_id  $c_scr_pmm_all $c_pre_pmm_all [pweight=wgt_strat2] if T_mixed_sect==1 & T_mixed_class==1, cmd($c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1: regress) match m(1) saving("$working/Leb_2_impute_`i'_3.dta", replace)   
		use "$working/Leb_2_impute_`i'_3.dta", clear
		keep if _mj==1
		rename _mj _mj3
		rename _mi _mi3
		ice $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1  i.set i.pre_q3_sect_REC3 ms_q1_mod_id  $c_scr_pmm_all $c_pre_pmm_all [pweight=wgt_strat2] if T_mixed_sect==0 & T_mixed_class==0, cmd($c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1: regress) match m(1) saving("$working/Leb_2_impute_`i'_4.dta", replace)   
		use "$working/Leb_2_impute_`i'_4.dta", clear
		keep if _mj==1
		rename _mj _mj4
		rename _mi _mi4
		ice $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1  i.set i.pre_q3_sect_REC3 ms_q1_mod_id  $c_scr_pmm_all $c_pre_pmm_all [pweight=wgt_strat2] if T_mixed_sect==0 & T_mixed_class==1, cmd($c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map1: regress) match m(1) saving("$working/Leb_2_impute_`i'_5.dta", replace)   
		use "$working/Leb_2_impute_`i'_5.dta", clear
		keep if _mj==1
		save "$working/Leb_2_impute_`i'_6.dta", replace
		
	}

*Combine MI datasets
	clear
	g m_impute = 0
	forval i = 0/10 { 
		append using "$working/Leb_2_impute_`i'_6.dta", nol
		replace m_impute = `i' if m_impute == .
		}

		
	mi import flong, m(m_impute) id(pid) clear

	misstable sum $c_scr_cat $c_pre_cat $c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map_all if m_impute==0

*Remember that categorical variables were not imputed
	misstable sum $c_scr_cat $c_pre_cat $c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map_all if m_impute>0

	mi register imputed $c_scr_pmm_all $c_pre_pmm_all $y_post_pmm_all $y_pet_pmm_all $y_pgg_pmm_all $y_map_all

	save "$data_newimp/Leb_2_Impute_FINAL.dta", replace
	
	
	






